package cn.itcast.usermanage.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private NewUserMapper NewuserMapper;
	
	private UserMapper userMapper;
	

	@Override
	public EasyUIResult queryEasyUIResult(Integer pageNum, Integer pageSize) {
		//在查询方法调用之前，调用分页插件的静态方法，中间最好不要隔任何代码
		PageHelper.startPage(pageNum, pageSize);
		//List<User> userlist=this.userMapper.queryUsersByPage((pageNum-1)*pagesize,pagesize);
		User user=new User();
		List<User> userlist=this.NewuserMapper.select(user);
		//初始化pageinfo对象，所有的分页参数都可以在对象中获取
		PageInfo<User> pageInfo=new PageInfo<User>(userlist);
		EasyUIResult easyUIResult=new EasyUIResult();
		easyUIResult.setTotal(pageInfo.getTotal());
		easyUIResult.setRows(pageInfo.getList());
		return easyUIResult;
	}


	@Override
	public void addUsers(User user1, User user2) {
		this.NewuserMapper.insertSelective(user1);
		//int i=1/0;
		this.NewuserMapper.insertSelective(user2);
		
	}


	@Override
	public Boolean addUser(User user) {
		user.setCreated(new Date());
		user.setUpdated(new Date());
		int count=this.NewuserMapper.insertSelective(user);
		if(count>0){
			return true;
		}
		return false;
	}
	@Override
	public Boolean deleteUser(List<Object> ids) {
		Example example=new Example(User.class);
		Criteria criteria=example.createCriteria();
		criteria.andIn("id", ids);
		int index = this.NewuserMapper.deleteByExample(example);
		if (index>0) {
			return true;
		}
		return false;
	}


	@Override
	public Boolean updateUser(User user) {
		user.setUpdated(new Date());
		int count = this.NewuserMapper.updateByPrimaryKey(user);
		if(count>0){
			return true;
		}
		return false;
	}


	@Override
	public User queryUserById(Long id) {
		return this.NewuserMapper.selectByPrimaryKey(id);
	}
   @Override
	public void save(User user) {
		this.NewuserMapper.insert(user);
	}




	
}
